package ln.javatest.day01.demo01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
//DDL创建一个表（DDL没有返回值，只有一个0）
public class JDBCDemo02 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try {
            //1.驱动注册
            Class.forName("com.mysql.jdbc.Driver");
            //2.定义sql
            String sql = "create table stu(id int,name varchar(20))";
            //3.获取Connection对象
            conn = DriverManager.getConnection("jdbc:mysql:///db1","root","123456");
            //4.获取执行sql的对象Statement
            stmt = conn.createStatement();
            //5.执行sql
            int count = stmt.executeUpdate(sql); //DDL没有返回值，只有0
            //6.处理结果
            System.out.println(count);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally{
            if(stmt != null){
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
